package cn.edu.zjicm.wordsnet_d.app;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zjicm.wordsnet_d.data.OneWord;

/**
 * 因为推送服务XMPushService在AndroidManifest.xml中设置为运行在另外一个进程，这导致本Application会被实例化两次，
 * 所以我们需要让非推送服务进程初始化。
 * 
 * @author cnting
 * 
 */
public class WordApp {

	private WordApp() {
		mWordChangeListenerList = new ArrayList<WordApp.OnWordChangeListener>();
	}

	private static WordApp singleInstance;

	public static WordApp getInstance() {
		if (null == singleInstance) {
			singleInstance = new WordApp();
		}
		return singleInstance;
	}

	private List<OnWordChangeListener> mWordChangeListenerList;

	public void setOnWordChangeListener(OnWordChangeListener l) {
		mWordChangeListenerList.add(l);
	}

	public void removeOnWordChangeListener(OnWordChangeListener l) {
		mWordChangeListenerList.remove(l);
	}

	public interface OnWordChangeListener {
		public void onWordDegreeChange(int oldDegree, OneWord word);

		public void onInstantiateItem(int position);

		public void onAdapterReset(int position);
	}

	public void notifyWordDegreeChange(int oldDegree, OneWord word) {
		for (OnWordChangeListener listener : mWordChangeListenerList) {
			listener.onWordDegreeChange(oldDegree, word);
		}
	}

	public void notifyInstantiateItem(int position) {
		for (OnWordChangeListener listener : mWordChangeListenerList) {
			listener.onInstantiateItem(position);
		}
	}

	public void notifyAdapterReset(int position) {
		for (OnWordChangeListener listener : mWordChangeListenerList) {
			listener.onAdapterReset(position);
		}
	}
}
